home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 1998 #2 / K-CD 0298.iso / wing / timewing.cp_ / timewing.cp
Text File  |  1995-06-20  |  27KB  |  948 lines

  1. /**************************************************************************
  2.  
  3.     TIMEWING.CPP - A timing demo for WinG
  4.  
  5.  **************************************************************************/
  6. /**************************************************************************
  7.  
  8.     (C) Copyright 1994 Microsoft Corp.  All rights reserved.
  9.  
  10.     You have a royalty-free right to use, modify, reproduce and 
  11.     distribute the Sample Files (and/or any modified version) in 
  12.     any way you find useful, provided that you agree that 
  13.     Microsoft has no warranty obligations or liability for any 
  14.     Sample Application Files which are modified. 
  15.  
  16.  **************************************************************************/
  17.  
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include <commdlg.h>
  21. #include "timewing.h"
  22.  
  23. #include <string.h>
  24. #include <mmsystem.h>
  25. #include <fstream.h>
  26. #include <strstrea.h>
  27.  
  28. #include <wing.h>
  29. #include "..\utils\utils.h" 
  30.  
  31. /*----------------------------------------------------------------------------*\
  32. |                                        |
  33. | g l o b a l   v a r i a b l e s                        |
  34. |                                        |
  35. \*----------------------------------------------------------------------------*/
  36. static  char       szAppName[]="WinG Timer Sample";
  37. static  char       szAppFilter[]="Bitmaps\0*.bmp\0";
  38.  
  39. static  HINSTANCE  hInstApp;
  40. HWND               hwndApp;
  41. static  HPALETTE   hpalApp;
  42. static  BOOL       fAppActive;
  43.  
  44. static int         dx, dy;
  45.  
  46. PDIB               pCurrentDIB;
  47. char               aDescription[200];
  48. char               aBuffer[5000];
  49. UINT               CurrentDIBUsage;
  50.  
  51. struct
  52. {
  53.   BITMAPINFOHEADER Header;
  54.   RGBQUAD aColors[256];
  55. } Info;
  56.  
  57. int Iterations = 100;
  58. int StretchMenu = 0;
  59. float StretchFactor = 1;
  60.  
  61. /*----------------------------------------------------------------------------
  62.  
  63. Timers
  64.  
  65. */
  66.  
  67. struct timing_result;
  68. typedef void timer( timing_result *pResults, HWND Window );
  69.  
  70. timer TimeStretchBlt;
  71. timer TimeStretchDIBits;
  72. timer TimeWinGBU;
  73. timer TimeWinGTD;
  74.  
  75.  
  76. /*----------------------------------------------------------------------------
  77.  
  78. Timer structure.
  79.  
  80. */
  81.  
  82. void PrintTimingResults( ostream &Out );
  83.  
  84. struct timing_result
  85. {
  86.   DWORD Time;
  87.   timer *pTimer;
  88.   char const *pDescription;
  89. } aTimings[] =
  90. {
  91.   0, TimeStretchBlt, "StretchBlt",
  92.   0, TimeStretchDIBits, "StretchDIBits",
  93.   0, TimeWinGTD, "WinGStretchBlt top-down",
  94.   0, TimeWinGBU, "WinGStretchBlt bottom-up",
  95. };
  96.  
  97. int const NumberOfTimings = sizeof(aTimings) / sizeof(aTimings[0]);
  98.   
  99.  
  100.  
  101.   
  102. #if defined(WIN32) || defined(_WIN32)
  103.   #define _export
  104. #endif
  105.  
  106. /*----------------------------------------------------------------------------*\
  107. |                                        |
  108. | f u n c t i o n   d e f i n i t i o n s                    |
  109. |                                        |
  110. \*----------------------------------------------------------------------------*/
  111.  
  112. LONG FAR PASCAL _export  AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  113. int   ErrMsg (LPSTR sz,...);
  114. LONG  AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  115.  
  116. void  AppExit(void);
  117. BOOL  AppIdle(void);
  118. void  AppOpenFile(HWND hwnd, LPSTR szFileName);
  119.  
  120. /*----------------------------------------------------------------------------*\
  121. | AppAbout( hDlg, uiMessage, wParam, lParam )                  |
  122. |                                        |
  123. | Description:                                 |
  124. |   This function handles messages belonging to the "About" dialog box.    |
  125. |   The only message that it looks for is WM_COMMAND, indicating the use   |
  126. |   has pressed the "OK" button.  When this happens, it takes down       |
  127. |   the dialog box.                              |
  128. |                                        |
  129. | Arguments:                                   |
  130. |   hDlg      window handle of about dialog window           |
  131. |   uiMessage   message number                       |
  132. |   wParam      message-dependent                    |
  133. |   lParam      message-dependent                    |
  134. |                                        |
  135. | Returns:                                   |
  136. |   TRUE if message has been processed, else FALSE               |
  137. |                                        |
  138. \*----------------------------------------------------------------------------*/
  139. BOOL FAR PASCAL _export AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  140. {
  141.   switch (msg)
  142.   {
  143.     case WM_COMMAND:
  144.       if (LOWORD(wParam) == IDOK)
  145.       {
  146.         EndDialog(hwnd,TRUE);
  147.       }
  148.       break;
  149.  
  150.     case WM_INITDIALOG:
  151.       return TRUE;
  152.   }
  153.   return FALSE;
  154. }
  155.  
  156. /*----------------------------------------------------------------------------*\
  157. | AppInit( hInst, hPrev)                                                       |
  158. |                                                                              |
  159. | Description:                                                                 |
  160. |   This is called when the application is first loaded into                   |
  161. |   memory.  It performs all initialization that doesn't need to be done       |
  162. |   once per instance.                                                         |
  163. |                                                                              |
  164. | Arguments:                                                                   |
  165. |   hInstance   instance handle of current instance                            |
  166. |   hPrev       instance handle of previous instance                           |
  167. |                                                                              |
  168. | Returns:                                                                     |
  169. |   TRUE if successful, FALSE if not                                           |
  170. |                                                                              |
  171. \*----------------------------------------------------------------------------*/
  172. BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
  173. {
  174.   WNDCLASS cls;
  175.  
  176.   /* Save instance handle for DialogBoxes */
  177.   hInstApp = hInst;
  178.  
  179. // Clear the System Palette so that WinGStretchBlt runs at full speed.
  180.   ClearSystemPalette();
  181.   
  182.   if (!hPrev)
  183.   {
  184.     /*
  185.      *  Register a class for the main application window
  186.      */
  187.     cls.hCursor      = LoadCursor(NULL,IDC_ARROW);
  188.     cls.hIcon      = LoadIcon(hInst,"AppIcon");
  189.     cls.lpszMenuName   = "AppMenu";
  190.     cls.lpszClassName  = szAppName;
  191.     cls.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
  192.     cls.hInstance    = hInst;
  193.     cls.style      = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  194.     cls.lpfnWndProc    = (WNDPROC)AppWndProc;
  195.     cls.cbWndExtra     = 0;
  196.     cls.cbClsExtra     = 0;
  197.  
  198.     if (!RegisterClass(&cls))
  199.       return FALSE;
  200.   }
  201.  
  202.   dx = 400;
  203.   dy = 400;
  204.  
  205.   hwndApp = CreateWindow (szAppName,    // Class name
  206.               szAppName,                // Caption
  207.               WS_OVERLAPPEDWINDOW,      // Style bits
  208.               50, 50,                   // Position
  209.               dx+20,dy+75,              // Size
  210.               (HWND)NULL,               // Parent window (no parent)
  211.               (HMENU)NULL,              // use class menu
  212.               hInst,                    // handle to window instance
  213.               (LPSTR)NULL               // no params to pass on
  214.                );
  215.   ShowWindow(hwndApp,sw);
  216.  
  217.  
  218.     HMENU Menu = GetMenu(hwndApp);
  219.     CheckMenuItem(Menu,MENU_1TO1,MF_CHECKED);
  220.  
  221.     //
  222.     // build the timing menu.
  223.     //
  224.  
  225.     HMENU hmenu = GetSubMenu(Menu, 3);
  226.     DeleteMenu(hmenu, MENU_TIME, MF_BYCOMMAND);
  227.  
  228.     for (int i=0; i<NumberOfTimings; i++)
  229.     {
  230.         AppendMenu(hmenu, 0, MENU_TIME+i, aTimings[i].pDescription);
  231.     }
  232.  
  233.     hpalApp = WinGCreateHalftonePalette();
  234.     AppOpenFile(hwndApp,"frog.bmp");
  235.  
  236.     if(!pCurrentDIB)
  237.     {
  238.       // we couldn't load froggie
  239.  
  240.       PostMessage(hwndApp,WM_COMMAND,MENU_OPEN,0);
  241.     }
  242.  
  243.   return TRUE;
  244. }
  245.  
  246.  
  247. /*----------------------------------------------------------------------------*\
  248. | AppExit()                                                                    |
  249. |                                                                              |
  250. | Description:                                                                 |
  251. |   App is just about to exit, cleanup.                                        |
  252. |                                                                              |
  253. \*----------------------------------------------------------------------------*/
  254. void AppExit()
  255. {
  256.   DeleteObject(hpalApp);
  257. }
  258.  
  259. /*----------------------------------------------------------------------------*\
  260. | WinMain( hInst, hPrev, lpszCmdLine, cmdShow )                                |
  261. |                                                                              |
  262. | Description:                                                                 |
  263. |   The main procedure for the App.  After initializing, it just goes          |
  264. |   into a message-processing loop until it gets a WM_QUIT message             |
  265. |   (meaning the app was closed).                                              |
  266. |                                                                              |
  267. | Arguments:                                                                   |
  268. |   hInst     instance handle of this instance of the app                      |
  269. |   hPrev     instance handle of previous instance, NULL if first              |
  270. |   szCmdLine ->null-terminated command line                                   |
  271. |   cmdShow   specifies how the window is initially displayed                  |
  272. |                                                                              |
  273. | Returns:                                                                     |
  274. |   The exit code as specified in the WM_QUIT message.                         |
  275. |                                                                              |
  276. \*----------------------------------------------------------------------------*/
  277. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  278. {
  279.   MSG   msg;
  280.  
  281.   /* Call initialization procedure */
  282.   if (!AppInit(hInst,hPrev,sw,szCmdLine))
  283.   return FALSE;
  284.  
  285.   /*
  286.    * Polling messages from event queue
  287.    */
  288.   for (;;)
  289.   {
  290.     if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
  291.     {
  292.       if (msg.message == WM_QUIT)
  293.         break;
  294.  
  295.       TranslateMessage(&msg);
  296.       DispatchMessage(&msg);
  297.     }
  298.     else
  299.   {
  300.     if (AppIdle())
  301.         WaitMessage();
  302.     }
  303.   }
  304.  
  305.   AppExit();
  306.   return msg.wParam;
  307. }
  308.  
  309. /*----------------------------------------------------------------------------*\
  310. | AppIdle()                    
  311. |                                        
  312. | Description:                                 
  313. |   Place to do idle time stuff.               
  314. |                                        
  315. \*----------------------------------------------------------------------------*/
  316. BOOL AppIdle()
  317. {
  318.   if (fAppActive)
  319.   {
  320.   //
  321.   // we are the foreground app.
  322.   //
  323.   return TRUE;    // nothing to do.
  324.   }
  325.   else
  326.   {
  327.   //
  328.   // we are a background app.
  329.   //
  330.   return TRUE;    // nothing to do.
  331.   }
  332. }
  333.  
  334. /*----------------------------------------------------------------------------*\
  335. | AppOpenFile()                  
  336. |                                        
  337. | Description:                                 
  338. |   Open a file.                
  339. |                                        
  340. \*----------------------------------------------------------------------------*/
  341. void AppOpenFile(HWND hwnd, LPSTR szFileName)
  342. {
  343.   PDIB pDIB = DibOpenFile(szFileName);
  344.  
  345.   if(pDIB)
  346.   {
  347.     if(pCurrentDIB)
  348.     {
  349.       DibFree(pCurrentDIB);
  350.     }
  351.  
  352.     pCurrentDIB = pDIB;
  353.  
  354.      // Map the dib to the palette if it's not 4-bit
  355.  
  356.     if (DibBitCount(pDIB) != 4)
  357.     {
  358.       DibMapToPalette(pCurrentDIB,hpalApp);
  359.         CurrentDIBUsage = DIB_PAL_COLORS;
  360.  
  361.       hmemcpy(&Info,pDIB,sizeof(Info));
  362.  
  363.       DibSetUsage(pCurrentDIB,hpalApp,CurrentDIBUsage);
  364.     }
  365.      else
  366.      {
  367.        CurrentDIBUsage = DIB_RGB_COLORS;
  368.      }
  369.  
  370.     ostrstream Out(aDescription,sizeof(aDescription));
  371.  
  372.     while(*szFileName)
  373.     {
  374.       Out<<*szFileName++;   // can't use far * and near stream.
  375.     }
  376.     
  377.     Out<<" - "<<DibWidth(pDIB)<<" x "<<DibHeight(pDIB)<<ends;
  378.  
  379.     SetWindowText(hwnd,aDescription);
  380.   }
  381. }
  382.  
  383. /*----------------------------------------------------------------------------*\
  384. | AppPaint(hwnd, hdc)                              
  385. |                                        
  386. | Description:                                 
  387. |   The paint function.  
  388. |                                        
  389. | Arguments:                                   
  390. |   hwnd       window painting into            
  391. |   hdc        display context to paint to               
  392. |                                        
  393. \*----------------------------------------------------------------------------*/
  394. AppPaint (HWND hwnd, HDC hdc)
  395. {
  396.   RECT  rc;
  397.  
  398.   GetClientRect(hwnd,&rc);
  399.  
  400.   SetTextColor(hdc,GetSysColor(COLOR_WINDOWTEXT));
  401.   SetBkColor(hdc,GetSysColor(COLOR_WINDOW));
  402.  
  403.   return TRUE;
  404. }
  405.  
  406. /*----------------------------------------------------------------------------*\
  407. | AppWndProc( hwnd, uiMessage, wParam, lParam )                                |
  408. |                                                                              |
  409. | Description:                                                                 |
  410. |   The window proc for the app's main (tiled) window.  This processes all     |
  411. |   of the parent window's messages.                                           |
  412. |                                                                              |
  413. \*----------------------------------------------------------------------------*/
  414. LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  415. {
  416.   PAINTSTRUCT ps;
  417.   HDC hdc;
  418.   BOOL f;
  419.  
  420.   switch (msg)
  421.   {
  422.     case WM_CREATE:
  423.     break;
  424.  
  425.     case WM_ACTIVATEAPP:
  426.     fAppActive = (BOOL)wParam;
  427.     break;
  428.  
  429.     case WM_TIMER:
  430.       break;
  431.  
  432.     case WM_ERASEBKGND:
  433.       break;
  434.  
  435.     case WM_INITMENU:
  436.       break;
  437.  
  438.     case WM_COMMAND:
  439.       return AppCommand(hwnd,msg,wParam,lParam);
  440.  
  441.   case WM_DESTROY:
  442.       PostQuitMessage(0);
  443.       break;
  444.  
  445.     case WM_CLOSE:
  446.     break;
  447.  
  448.     case WM_SIZE:
  449.     {
  450.       static FirstTime = 1;
  451.  
  452.       if(FirstTime)
  453.       {
  454.         FirstTime = 0;
  455.         break;
  456.       }
  457.       else
  458.       {
  459.         return 0;
  460.         break;
  461.       }
  462.     }
  463.  
  464.     case WM_PALETTECHANGED:
  465.     if ((HWND)wParam == hwnd)
  466.     break;
  467.  
  468.     // fall through to WM_QUERYNEWPALETTE
  469.  
  470.   case WM_QUERYNEWPALETTE:
  471.     hdc = GetDC(hwnd);
  472.  
  473.     if (hpalApp)
  474.       SelectPalette(hdc, hpalApp, FALSE);
  475.  
  476.     f = RealizePalette(hdc);
  477.     ReleaseDC(hwnd,hdc);
  478.  
  479.     if (f)
  480.       InvalidateRect(hwnd,NULL,TRUE);
  481.  
  482.     return f;
  483.  
  484.     case WM_PAINT:
  485.     hdc = BeginPaint(hwnd,&ps);
  486.     AppPaint (hwnd,hdc);
  487.       EndPaint(hwnd,&ps);
  488.       return 0L;
  489.   }
  490.   return DefWindowProc(hwnd,msg,wParam,lParam);
  491. }
  492.  
  493. /*----------------------------------------------------------------------------*\
  494. | AppCommand(hwnd, msg, wParam, lParam )                                       |
  495. |                                                                              |
  496. | Description:                                                                 |
  497. |   Handles WM_COMMAND messages for the main window (hwndApp)                  |
  498. | of the parent window's messages.                                             |
  499. |                                                                              |
  500. \*----------------------------------------------------------------------------*/
  501. LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  502. {
  503.   static char achFileName[128];
  504.   OPENFILENAME ofn;
  505.   ostrstream Out(aBuffer,sizeof(aBuffer));
  506.   int PrevStretchMenu;       
  507.   int i;
  508.  
  509.   switch(wParam)
  510.   {
  511.     case MENU_ABOUT:
  512.     DialogBox(hInstApp,"AppAbout",hwnd,(DLGPROC)AppAbout);
  513.       break;
  514.  
  515.     case MENU_TIMEALL:
  516.     {
  517.       HCURSOR Arror = SetCursor(0);
  518.  
  519.       for(int Counter = 0;Counter < NumberOfTimings;Counter++)
  520.       {
  521.         SetWindowText(hwndApp,aTimings[Counter].pDescription);
  522.  
  523.         (*aTimings[Counter].pTimer)(aTimings+Counter,hwnd);
  524.       }
  525.  
  526.       SetCursor(Arror);
  527.       SetWindowText(hwndApp,aDescription);
  528.  
  529.       PrintTimingResults(Out);
  530.       MessageBox(0,aBuffer,"Timing Results",MB_OK);
  531.  
  532.       break;
  533.     }
  534.  
  535.     case MENU_1TO1:
  536.     case MENU_1TO2:
  537.     case MENU_1TOALMOST2:
  538.     {
  539.       float aStretchFactors[3] = { 1, 2, 1.9 };
  540.       HMENU Menu = GetMenu(hwnd);
  541.  
  542.       // uncheck current selection
  543.       CheckMenuItem(Menu,MENU_1TO1 + StretchMenu,MF_UNCHECKED);
  544.  
  545.       PrevStretchMenu = StretchMenu;
  546.  
  547.       StretchMenu = wParam - MENU_1TO1;
  548.  
  549.       // get the stretch factor
  550.       StretchFactor = aStretchFactors[StretchMenu];
  551.  
  552.       Iterations = (StretchFactor == 1) ? 100 : 20;
  553.  
  554.       CheckMenuItem(Menu,wParam,MF_CHECKED);
  555.  
  556.       if (StretchMenu != PrevStretchMenu)
  557.       {
  558.         for (i = 0; i < NumberOfTimings; i++)
  559.           aTimings[i].Time = 0;
  560.       }
  561.     
  562.       break;
  563.     }
  564.  
  565.     default:
  566.     {
  567.       int Index = (int)LOWORD(wParam) - MENU_TIME;
  568.  
  569.       if(Index >= 0 && Index < NumberOfTimings)
  570.       {
  571.         HCURSOR Arror = SetCursor(0);
  572.  
  573.         SetWindowText(hwndApp,aTimings[Index].pDescription);
  574.         
  575.         (*aTimings[Index].pTimer)(aTimings+Index,hwnd);
  576.  
  577.         SetCursor(Arror);
  578.         SetWindowText(hwndApp,aDescription);
  579.       }
  580.  
  581.       PrintTimingResults(Out);
  582.       MessageBox(0,aBuffer,"Timing Results",MB_OK);
  583.       InvalidateRect(hwnd,0,TRUE);
  584.       UpdateWindow(hwnd);
  585.  
  586.       break;
  587.     }
  588.  
  589.   case MENU_OPEN:
  590.   {
  591.       /* prompt user for file to open */
  592.       ofn.lStructSize = sizeof(OPENFILENAME);
  593.       ofn.hwndOwner = hwnd;
  594.       ofn.hInstance = NULL;
  595.       ofn.lpstrFilter = szAppFilter;
  596.       ofn.lpstrCustomFilter = NULL;
  597.       ofn.nMaxCustFilter = 0;
  598.       ofn.nFilterIndex = 0;
  599.       ofn.lpstrFile = achFileName;
  600.       ofn.nMaxFile = sizeof(achFileName);
  601.       ofn.lpstrFileTitle = NULL;
  602.       ofn.nMaxFileTitle = 0;
  603.       ofn.lpstrInitialDir = NULL;
  604.       ofn.lpstrTitle = NULL;
  605.       ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  606.       ofn.nFileOffset = 0;
  607.       ofn.nFileExtension = 0;
  608.       ofn.lpstrDefExt = NULL;
  609.       ofn.lCustData = 0;
  610.       ofn.lpfnHook = NULL;
  611.       ofn.lpTemplateName = NULL;
  612.  
  613.       if(GetOpenFileName(&ofn))
  614.       {
  615.         AppOpenFile(hwnd,achFileName);
  616.         break;
  617.       }
  618.   }
  619.  
  620.       break;
  621.  
  622.     case MENU_EXIT:
  623.       PostMessage(hwnd,WM_CLOSE,0,0L);
  624.       break;
  625.   }
  626.   return 0L;
  627. }
  628.  
  629.  
  630.  
  631.  
  632. /*----------------------------------------------------------------------------*\
  633. | ErrMsg - Opens a Message box with a error message in it.  The user can       |
  634. |      select the OK button to continue                                        |
  635. \*----------------------------------------------------------------------------*/
  636. int ErrMsg (LPSTR sz,...)
  637. {
  638.   char ach[128];
  639.  
  640.   wvsprintf (ach,sz,(LPSTR)(&sz+1));   /* Format the string */
  641.   MessageBox(hwndApp,ach,szAppName,MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
  642.   return FALSE;
  643. }
  644.  
  645.  
  646. void PrintTimingResults( ostream &Out )
  647. {
  648.   int CurrentOffset = 0;
  649.  
  650.   for(int Counter = 0;Counter < NumberOfTimings;Counter++)
  651.   {
  652.     Out<<aTimings[Counter].pDescription
  653.       <<":\t";
  654.  
  655.     if(aTimings[Counter].Time)
  656.     {
  657.       Out.setf(ios::fixed | ios::showpoint);
  658.       Out.width(6);
  659.       Out.precision(2);
  660.       Out<<(Iterations / (aTimings[Counter].Time / 1000.0))<<" per Second"<<endl;
  661.     }
  662.     else
  663.     {
  664.       Out<<"Not timed yet"<<endl;
  665.     }
  666.     
  667.   }
  668.  
  669.   Out<<ends;
  670.  
  671. }
  672.  
  673.  
  674.  
  675. /*----------------------------------------------------------------------------
  676.  
  677. StretchBlt
  678.  
  679. */
  680.  
  681. void TimeStretchBlt( timing_result *pResult, HWND Window )
  682. {
  683.   int Counter;
  684.   HDC Screen = GetDC(Window);
  685.   DWORD Time;
  686.  
  687.   // setup here
  688.  
  689.   HDC Buffer = CreateCompatibleDC(Screen);
  690.   HBITMAP Bitmap = CreateCompatibleBitmap(Screen,DibWidth(pCurrentDIB),
  691.             DibHeight(pCurrentDIB));
  692.   Bitmap = SelectBitmap(Buffer,Bitmap);
  693.  
  694.   HPALETTE OldPalette = SelectPalette(Buffer,hpalApp,FALSE);
  695.   SelectPalette(Screen,hpalApp,FALSE);
  696.   RealizePalette(Screen);
  697.   RealizePalette(Buffer);
  698.   SetStretchBltMode(Buffer,COLORONCOLOR);
  699.  
  700.   StretchDIBits(Buffer,0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),
  701.     0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),DibPtr(pCurrentDIB),
  702.     DibInfo(pCurrentDIB),CurrentDIBUsage,SRCCOPY);
  703.  
  704.   SetStretchBltMode(Screen,COLORONCOLOR);
  705.  
  706.   int Width = StretchFactor * DibWidth(pCurrentDIB);
  707.   int Height = StretchFactor * DibHeight(pCurrentDIB);
  708.   
  709.   Time = timeGetTime();
  710.  
  711.   for(Counter = 0;Counter < Iterations;Counter++)
  712.   {
  713.     StretchBlt(Screen,Counter,Counter,Width,Height,
  714.       Buffer,0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),
  715.       SRCCOPY);
  716.   }
  717.  
  718.   Time = timeGetTime() - Time;
  719.  
  720.   pResult->Time = Time;
  721.  
  722.   // clean up
  723.  
  724.   SelectPalette(Buffer,OldPalette,FALSE);
  725.   SelectPalette(Screen,OldPalette,FALSE);
  726.   DeleteObject(SelectBitmap(Buffer,Bitmap));
  727.   DeleteDC(Buffer);
  728.  
  729.   ReleaseDC(Window,Screen);
  730. }
  731.  
  732.  
  733.  
  734. /*----------------------------------------------------------------------------
  735.  
  736. StretchDIBits
  737.  
  738. */
  739.  
  740. void TimeStretchDIBits( timing_result *pResult, HWND Window )
  741. {
  742.   int Counter;
  743.   HDC Screen = GetDC(Window);
  744.   DWORD Time;
  745.  
  746.   // setup here
  747.  
  748.   HPALETTE OldPalette = SelectPalette(Screen,hpalApp,FALSE);
  749.   RealizePalette(Screen);
  750.  
  751.   int Width = StretchFactor * DibWidth(pCurrentDIB);
  752.   int Height = StretchFactor * DibHeight(pCurrentDIB);
  753.   
  754.   Time = timeGetTime();
  755.  
  756.   for(Counter = 0;Counter < Iterations;Counter++)
  757.   {
  758.     StretchDIBits(Screen,Counter,Counter,Width,Height,
  759.       0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),
  760.       DibPtr(pCurrentDIB),DibInfo(pCurrentDIB),CurrentDIBUsage,SRCCOPY);
  761.   }
  762.  
  763.   Time = timeGetTime() - Time;
  764.  
  765.   pResult->Time = Time;
  766.  
  767.   // clean up
  768.  
  769.   SelectPalette(Screen,OldPalette,FALSE);
  770.  
  771.   ReleaseDC(Window,Screen);
  772. }
  773.  
  774. /*----------------------------------------------------------------------------
  775.  
  776. WinGStretchBlt - bottom-up
  777.  
  778. */
  779.  
  780. void TimeWinGBU( timing_result *pResult, HWND Window )
  781. {
  782.   int Counter;
  783.   HDC Screen = GetDC(Window);
  784.   DWORD Time;
  785.  
  786.   void far *pBits;
  787.  
  788.   // setup here
  789.  
  790.   HDC WinGDC = WinGCreateDC();
  791.  
  792.   Info.Header.biSize = sizeof(BITMAPINFOHEADER);
  793.   Info.Header.biWidth = DibWidth(pCurrentDIB);
  794.  
  795.   // NOTE!  You should use the WinGRecommendedDIBFormat for your WinGBitmaps.
  796.   // This is forced to bottom-up for this example.  See the WinG.HLP file.
  797.   
  798.   Info.Header.biHeight = DibHeight(pCurrentDIB);
  799.  
  800.   HBITMAP WinGBitmap = WinGCreateBitmap(WinGDC,(BITMAPINFO far *)&Info,
  801.                 &pBits);
  802.  
  803.   WinGBitmap = SelectBitmap(WinGDC,WinGBitmap);
  804.  
  805.   HPALETTE OldPalette = SelectPalette(Screen,hpalApp,FALSE);
  806.   RealizePalette(Screen);
  807.  
  808.   // We're selecting a palette into the WinGDC so we can use the
  809.   // DIB_PAL_COLORS pCurrentDIB.  This does NOT change the WinGBitmap's
  810.   // color table, but GDI needs to look up the DIB_PAL_COLORS somewhere!
  811.   // Different devices can't share a palette, so we need another copy.
  812.  
  813.   HPALETTE WinGPalette = WinGCreateHalftonePalette();
  814.   
  815.   HPALETTE OldWinGPalette = SelectPalette(WinGDC,WinGPalette,FALSE);
  816.   RealizePalette(WinGDC);
  817.  
  818.   SetStretchBltMode(WinGDC,COLORONCOLOR);
  819.  
  820.   StretchDIBits(WinGDC,0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),
  821.     0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),DibPtr(pCurrentDIB),
  822.     DibInfo(pCurrentDIB),CurrentDIBUsage,SRCCOPY);
  823.  
  824.   // we just needed the palette for the StretchDIBits
  825.   
  826.   SelectPalette(WinGDC,OldWinGPalette,FALSE);
  827.   DeleteObject(WinGPalette);
  828.   
  829.   int Width = StretchFactor * DibWidth(pCurrentDIB);
  830.   int Height = StretchFactor * DibHeight(pCurrentDIB);
  831.  
  832.   // WinG does a lot of work on the first blt, so do it outside the loop
  833.  
  834.   WinGStretchBlt(Screen,0,0,Width,Height,
  835.       WinGDC,0,0,DibWidth(pCurrentDIB),
  836.       DibHeight(pCurrentDIB));
  837.  
  838.   Time = timeGetTime();
  839.  
  840.   for(Counter = 0;Counter < Iterations;Counter++)
  841.   {
  842.     WinGStretchBlt(Screen,Counter,Counter,Width,Height,
  843.       WinGDC,0,0,DibWidth(pCurrentDIB),
  844.       DibHeight(pCurrentDIB));
  845.   }
  846.  
  847.   Time = timeGetTime() - Time;
  848.  
  849.   pResult->Time = Time;
  850.  
  851.   // clean up
  852.  
  853.   SelectPalette(Screen,OldPalette,FALSE);
  854.   SelectPalette(WinGDC,OldPalette,FALSE);
  855.   DeleteObject(SelectBitmap(WinGDC,WinGBitmap));
  856.   DeleteDC(WinGDC);
  857.  
  858.   ReleaseDC(Window,Screen);
  859. }
  860.  
  861.  
  862. /*----------------------------------------------------------------------------
  863.  
  864. WinGStretchBlt - top-down
  865.  
  866. */
  867.  
  868. void TimeWinGTD( timing_result *pResult, HWND Window )
  869. {
  870.   int Counter;
  871.   HDC Screen = GetDC(Window);
  872.   DWORD Time;
  873.  
  874.   void far *pBits;
  875.  
  876.   // setup here
  877.  
  878.   HDC WinGDC = WinGCreateDC();
  879.  
  880.   Info.Header.biSize = sizeof(BITMAPINFOHEADER);
  881.   Info.Header.biWidth = DibWidth(pCurrentDIB);
  882.  
  883.   // NOTE!  You should use the WinGRecommendedDIBFormat for your WinGBitmaps.
  884.   // This is forced to top-down for this example.  See the WinG.HLP file.
  885.   
  886.   Info.Header.biHeight = -int(DibHeight(pCurrentDIB));
  887.  
  888.   HBITMAP WinGBitmap = WinGCreateBitmap(WinGDC,(BITMAPINFO far *)&Info,
  889.                 &pBits);
  890.  
  891.   WinGBitmap = SelectBitmap(WinGDC,WinGBitmap);
  892.  
  893.   HPALETTE OldPalette = SelectPalette(Screen,hpalApp,FALSE);
  894.   RealizePalette(Screen);
  895.  
  896.   // We're selecting a palette into the WinGDC so we can use the
  897.   // DIB_PAL_COLORS pCurrentDIB.  This does NOT change the WinGBitmap's
  898.   // color table, but GDI needs to look up the DIB_PAL_COLORS somewhere!
  899.   // Different devices can't share a palette, so we need another copy.
  900.  
  901.   HPALETTE WinGPalette = WinGCreateHalftonePalette();
  902.   
  903.   HPALETTE OldWinGPalette = SelectPalette(WinGDC,WinGPalette,FALSE);
  904.   RealizePalette(WinGDC);
  905.  
  906.   SetStretchBltMode(WinGDC,COLORONCOLOR);
  907.  
  908.   StretchDIBits(WinGDC,0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),
  909.     0,0,DibWidth(pCurrentDIB),DibHeight(pCurrentDIB),DibPtr(pCurrentDIB),
  910.     DibInfo(pCurrentDIB),CurrentDIBUsage,SRCCOPY);
  911.  
  912.   // we just needed the palette for the StretchDIBits
  913.   
  914.   SelectPalette(WinGDC,OldWinGPalette,FALSE);
  915.   DeleteObject(WinGPalette);
  916.   
  917.   int Width = StretchFactor * DibWidth(pCurrentDIB);
  918.   int Height = StretchFactor * DibHeight(pCurrentDIB);
  919.  
  920.   // WinG does a lot of work on the first blt, so do it outside the loop
  921.  
  922.   WinGStretchBlt(Screen,0,0,Width,Height,
  923.       WinGDC,0,0,DibWidth(pCurrentDIB),
  924.       DibHeight(pCurrentDIB));
  925.  
  926.   Time = timeGetTime();
  927.  
  928.   for(Counter = 0;Counter < Iterations;Counter++)
  929.   {
  930.     WinGStretchBlt(Screen,Counter,Counter,Width,Height,
  931.       WinGDC,0,0,DibWidth(pCurrentDIB),
  932.       DibHeight(pCurrentDIB));
  933.   }
  934.  
  935.   Time = timeGetTime() - Time;
  936.  
  937.   pResult->Time = Time;
  938.  
  939.   // clean up
  940.  
  941.   SelectPalette(Screen,OldPalette,FALSE);
  942.   SelectPalette(WinGDC,OldPalette,FALSE);
  943.   DeleteObject(SelectBitmap(WinGDC,WinGBitmap));
  944.   DeleteDC(WinGDC);
  945.  
  946.   ReleaseDC(Window,Screen);
  947. }
  948.